package cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.service.lottery;

import cn.iocoder.yudao.framework.common.exception.ServiceException;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.dataobject.goods.GoodsDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.goodInfo.goods.dal.mysql.goods.GoodsMapper;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.controller.admin.lottery.vo.LotteryCreateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.controller.admin.lottery.vo.LotteryExportReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.controller.admin.lottery.vo.LotteryPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.controller.admin.lottery.vo.LotteryUpdateReqVO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.convert.lottery.LotteryConvert;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.dal.dataobject.lottery.LotteryDO;
import cn.iocoder.yudao.module.system.controller.admin.mini.other.lottery.dal.mysql.lottery.LotteryMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.framework.common.pojo.PageResult;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 抽奖 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class LotteryServiceImpl implements LotteryService {

    @Resource
    private LotteryMapper lotteryMapper;
    @Resource
    GoodsMapper goodsMapper;

    @Override
    public Integer createLottery(LotteryDO lottery) {
        if (lottery.getTid()==null)
            throw new ServiceException(500,"请选择对应的转盘");
        // 插入
        lotteryMapper.insert(lottery);
        // 返回
        return lottery.getId();
    }

    @Override
    public void updateLottery(LotteryDO updateReqVO) {
        // 校验存在
        validateLotteryExists(updateReqVO.getId());
        // 更新
        lotteryMapper.updateById(updateReqVO);
    }

    @Override
    public void deleteLottery(Integer id) {
        // 校验存在
        validateLotteryExists(id);
        // 删除
        lotteryMapper.deleteById(id);
    }

    private void validateLotteryExists(Integer id) {
        if (lotteryMapper.selectById(id) == null) {
            throw exception(500);
        }
    }

    @Override
    public LotteryDO getLottery(Integer id) {
        LotteryDO vo = lotteryMapper.selectById(id);
        if (vo.getType()==1){
            GoodsDO goodsDO = goodsMapper.selectById(vo.getGoodsId());
            if (goodsDO!=null){
                vo.setGoodsName(goodsDO.getName());
                vo.setStatus(goodsDO.getStatus());
                if (goodsDO.getImg()!=null&&goodsDO.getImg().size()>0)
                    vo.setZhanimg(goodsDO.getImg().get(0));
            }
        }
        return vo;
    }

    @Override
    public List<LotteryDO> getLotteryList(Collection<Integer> ids) {
        return lotteryMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<LotteryDO> getLotteryPage(LotteryPageReqVO pageReqVO) {
        return lotteryMapper.selectPage(pageReqVO);
    }

    @Override
    public List<LotteryDO> getLotteryList(LotteryExportReqVO exportReqVO) {
        return lotteryMapper.selectList(exportReqVO);
    }

}
